package com.dragon.repository.mapper;

import com.dragon.repository.entity.Role;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 角色表 Mapper 接口
 * </p>
 *
 * @author sy
 * @since 2023-06-12
 */
@Mapper
public interface RoleMapper extends BaseMapper<Role> {


    @Select("SELECT N.data_auth_type FROM " +
            "(SELECT A.role_id FROM dragon_admin_role A,dragon_admin B WHERE A.admin_id = B.id AND A.admin_id = #{id}) M,dragon_role N WHERE M.role_id = N.id")
    Role selectRoleListByAdminId(Integer id);


    @Select("SELECT N.id,N.data_auth_type FROM (SELECT role_id FROM dragon_role_menu A JOIN dragon_menu B ON A.menu_id = B.id WHERE B.path = #{path})M,dragon_role N " +
            "WHERE N.id = M.role_id" +
            "<if test='roleNames.size!=0'>" +
            "AND N.id in" +
            "<foreach collection = 'roleNames' item = 'item' open = '(' close = ')' separator = ','>" +
            "#{item}" +
            "</foreach>" +
            "</if>")
    List<Role> selectDataAuthByRoleName(List<String> roleNames,String path);


    @Select("select b.* from dragon_admin_role a,dragon_role b where a.role_id = b.id and a.admin_id = #{id} and b.enable = 1")
    Role selectRoleInfoByAdminId(Integer id);

    @Select("select dr.data_auth_type from dragon_role dr " +
            "left join " +
            "(select role_id from dragon_role_menu drm left join dragon_menu dm on drm.menu_id = dm.id where path = #{path}) dmmm " +
            "on dr.id = dmmm.role_id " +
            "left join dragon_admin_role dar on dar.role_id = dr.id " +
            "where dar.admin_id = #{adminId} order by dr.data_auth_type desc limit 1 ")
    Role selectAuthDataCode(Integer adminId,String path);
}
